package com.ebt.m.proposal_v2.ui;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.ebt.m.R;
import com.ebt.m.fragment.LazyFragment;
import com.ebt.m.proposal_v2.adapter.MainProductAdapter;
import com.ebt.m.proposal_v2.base.Constant;
import com.ebt.m.proposal_v2.dao.request.ParamGetRisks;
import com.ebt.m.proposal_v2.dao.response.ResponseMain;
import com.ebt.m.proposal_v2.helper.PickerHelper;
import com.ebt.m.proposal_v2.helper.ThemeHelper;
import com.ebt.m.proposal_v2.mvp.contract.CommonProductContract;
import com.ebt.m.proposal_v2.mvp.presenter.CommonProductPresenter;
import com.ebt.m.proposal_v2.mvp.presenter.MainProductPresenter;
import com.ebt.m.proposal_v2.widget.dialog.EBTProgressDialog;
import com.ebt.m.view.RecyclerViewItemDivider;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 常用产品列表
 *
 * @author archer.qi
 *         Created on 2017/5/23.
 */
public class CommonProductFragment extends LazyFragment implements CommonProductContract.View {

    @BindView(R.id.common_products_list)
    RecyclerView mRecyclerView;
    @BindView(R.id.common_products_empty)
    View mEmptyView;
    @BindView(R.id.empty_container)
    LinearLayout mEmptyContentContainer;
    @BindView(R.id.product_empty_content)
    TextView mEmptyContent;
    @BindView(R.id.no_net_container)
    RelativeLayout noNetContainer;
    @BindView(R.id.net_slow_container)
    RelativeLayout netSlowContainer;
    @BindView(R.id.load_refresh)
    TextView loadRefresh;
    @BindView(R.id.load_refresh_net)
    TextView loadRefreshNet;

    private ParamGetRisks mArg;
    private EBTProgressDialog mProgressDialog;
    private MainProductAdapter mAdapter;
    private CommonProductPresenter mPresenter;

    @Override
    protected void onCreateViewLazy(Bundle savedInstanceState) {
        super.onCreateViewLazy(savedInstanceState);
        View contentView = inflater.inflate(R.layout.fragment_common_products, null);
        ButterKnife.bind(this, contentView);
        setContentView(contentView);
        init();
        onDataSyncPost();
        createPresenter();
    }

    private void init() {
        initArguments();
        initEmptyView();
        initRecyclerView();
    }

    @Override
    public void initArguments() {
        MainProductActivity act = (MainProductActivity) getActivity();
        if (act != null) {
            mArg = act.getArgument();
        }
    }

    @Override
    public void setEmptyView(boolean isEmpty) {
    }

    @Override
    public void initRecyclerView() {
        mRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
        mRecyclerView.addItemDecoration(new RecyclerViewItemDivider(getActivity(), LinearLayoutManager.VERTICAL)
                .setAppearance(Constant.DIVIDER_SIZE, Constant.DIVIDER_COLOR_STRING));
    }

    @Override
    public void createPresenter() {
        mPresenter = new CommonProductPresenter(getApplicationContext(), this);
        mPresenter.start();
    }

    @Override
    public void initEmptyView() {
        int type = getCompanyType();
        if (type == MainProductPresenter.COMPANY_TYPE_CRE) {
            mEmptyContent.setText(ThemeHelper.getString(getActivity(), R.string.common_products_null_tip_cre));
        }
        if (type == MainProductPresenter.COMPANY_TYPE_INS) {
            mEmptyContent.setText(ThemeHelper.getString(getActivity(), R.string.common_products_null_tip_ins));
        }
    }

    @Override
    public void showProgress() {
        mProgressDialog = PickerHelper.showProgress(getActivity(), "加载常用产品");
    }

    @Override
    public void cancelProgress() {
        if (mProgressDialog != null) {
            mProgressDialog.dismiss();
        }
    }

    @Override
    public int getCompanyType() {
        MainProductActivity act = (MainProductActivity) getActivity();
        if (act != null) {
            MainProductPresenter presenter = act.getPresenter();
            if (presenter != null) {
                return presenter.getCompanyType();
            }
        }
        return -1;
    }


    @Override
    public void bindRecyclerData(List<ResponseMain> listData) {
        mRecyclerView.setVisibility(View.VISIBLE);
        mAdapter = new MainProductAdapter(getActivity(), listData, mArg);
        mAdapter.setLabelMode(true);
        mAdapter.showBrandLabel(true);
        mRecyclerView.setAdapter(mAdapter);
    }

    @Override
    public ParamGetRisks getArgument() {
        return mArg;
    }

    @Override
    public void finishActivity() {
        getActivity().finish();
    }

    @Override
    public void showEmpty(boolean empty, boolean no_net, boolean net_slow) {
        mEmptyView.setVisibility(View.VISIBLE);
        mEmptyContentContainer.setVisibility(empty ? View.VISIBLE : View.GONE);
        noNetContainer.setVisibility(no_net ? View.VISIBLE : View.GONE);
        netSlowContainer.setVisibility(net_slow ? View.VISIBLE : View.GONE);
    }

    @OnClick({R.id.load_refresh, R.id.load_refresh_net})
    public void onRefreshClicked(View v) {
        if (mPresenter != null) {
            mPresenter.start();
        }
    }


}
